/**  
* @author hongchun.li  
* @date 2013-5-6 下午10:53:01
* @version V1.0  
*/ 
package com.apis.security;

import java.util.Collection;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;

import com.apis.security.entity.impl.UserImpl;
import com.apis.security.entries.impl.UserMgrImpl;

/**
 * @author hongchun.li
 *
 */
public class UserRealm extends AuthorizingRealm{

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		  
	      System.out.print("@@@@@@@@@@@@@");
	      
		  return null; 
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken ) throws AuthenticationException {
		
		System.out.print("doGetAuthenticationInfo");
		UsernamePasswordToken upToken = (UsernamePasswordToken) authcToken;
		String username = upToken.getUsername();
		String password = upToken.getPassword().toString();
		
		Subject currentUser = SecurityUtils.getSubject();
		if(!username.equalsIgnoreCase("admin")){
			throw new UnknownAccountException("count not find user : " + username);
		}
		if(!password.equals("admin")){
			throw new IncorrectCredentialsException("password is incorrect.");
		}
		//try{
//			currentUser.login(authcToken);
//		}catch( UnknownAccountException e){
//			throw new UnknownAccountException("count not find user : " + username);
//		}catch( IncorrectCredentialsException e){
//			throw new UnknownAccountException("password is incorrect.");
//		}catch( LockedAccountException e ){
//			throw new UnknownAccountException("account is locked.");
//		}catch( AuthenticationException e){
//			throw new AuthenticationException("AuthenticationException");
//		}
		
		return null;
		
	}

}
